﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using Styx;
using Styx.Combat.CombatRoutine;
using Styx.Helpers;
using Styx.Logic;
using Styx.Logic.BehaviorTree;
using Styx.Logic.Combat;
using Styx.Logic.Pathing;
using Styx.Logic.POI;
using Styx.WoWInternals.WoWObjects;
using TreeSharp;
using Action = TreeSharp.Action;
using Sequence = TreeSharp.Sequence;
using Styx.WoWInternals;

namespace Healz.Druid.Spells
{
    class Corruption
    {
        public const string SpellName = "Remove Corruption";

        public static bool Pulse()
        {
            // Checks this is ugly cause im really not thinking atm
            if (ObjectManager.Me.IsInInstance || Battlegrounds.IsInsideBattleground) { } else { return false; }
            if (!SpellManager.Spells[SpellName].CanCast) return false;

            WoWPlayer PlayerToRemove = 
                (from Unit in ObjectManager.GetObjectsOfType<WoWPlayer>(true, true)
                    where Unit.IsFriendly
                    where !Unit.Dead
                    where Unit.Distance2D < 40
                    where Unit.CurrentHealth > 1
                    where NeedsCleanse(Unit)
                    select Unit).FirstOrDefault();

            if (PlayerToRemove == null) return false;


            // Cast and write
            Utils.WriteCast(SpellName, PlayerToRemove);
            SpellManager.Cast(SpellName, PlayerToRemove);

            StyxWoW.SleepForLagDuration();

            return false;
        }


        private static bool NeedsCleanse(WoWPlayer Player)
        {
            foreach (WoWAura Aura in Player.ActiveAuras.Values)
            {
                if (!Aura.IsActive) return false;
                if (!Aura.IsHarmful) return false;

                // Get the type
                WoWDispelType Type = Aura.Spell.DispelType;

                // If we can remove it
                if (Type == WoWDispelType.Curse 
                    || Type == WoWDispelType.Magic 
                    || Type == WoWDispelType.Poison)
                {
                    return true;
                }
            }
            return false;
        }
        
    }
}
